package com.rajeshDyadav.email;

import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class EMail extends Authenticator
{
    public static void main(String args[]) {
	EMail e = new EMail();
	e.sendMail();
    }

    public void sendMail()
    {
	// Recipient's email ID needs to be mentioned.
	String to = "base@baseinfo.com";

	// Sender's email ID needs to be mentioned
	String from = "base@baseinfo.com";

	// Assuming you are sending email from localhost
	String host = "127.0.0.1";

	// Get system properties
	Properties properties = System.getProperties();

	// Setup mail server
	properties.setProperty("mail.smtp.host", host);
	properties.setProperty("mail.smtp.port", "25");

	// Get the default Session object.
	Session session = Session.getInstance(properties, new SocialAuth());

	try {
	    // Create a default MimeMessage object.
	    MimeMessage message = new MimeMessage(session);
	    // Set From: header field of the header.
	    message.setFrom(new InternetAddress(from));
	    // Set To: header field of the header.
	    message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
	    // Set Subject: header field
	    message.setSubject("This is the Subject Line!");
	    // Now set the actual message
	    message.setText("This is actual message");

	    // Send message
	    System.out.println("Sending Mail....");
	    Transport.send(message);
	    System.out.println("Sent message successfully....");
	} catch (MessagingException mex) {
	    mex.printStackTrace();
	}
    }

    class SocialAuth extends Authenticator {
	@Override
	protected PasswordAuthentication getPasswordAuthentication() {
	    return new PasswordAuthentication("base@baseinfo.com", "77492652");
	}
    }
}